<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="https://cdn.staticfile.org/vue/2.7.0/vue.min.js"></script>
    <div id="box">
        <h1>商品列表</h1>
        <ul>
            <li v-for="(item,index) in arr" :key="item.id">
                <span>{{item.name}}</span>
                <span>{{item.price | n()}}</span>
                <button @click="add(item.id)">添加到购物车</button>
            </li>
        </ul>
        <h1>购物车</h1>
        <input type="checkbox" v-model="quanxuan" @change="xuan()">全选
        <ul>
            <li v-for="(item,index) in arr2" :key="item.id">
                <input type="checkbox" v-model="item.is" @change="inps(item.id)">
                <span>{{item.name}}</span>
                <span>{{item.price | n()}}</span>
                <button @click="jia(item.id)">+</button>
                <span>{{item.num}}</span>
                <button @click="jian(item.id)">-</button>
                <button @click="del(index)">删除</button>
            </li>
        </ul>
        <button @click="deltrue()">删除选中</button>
        <div>{{jisuan | n() + "元"}}</div>
    </div>
    <script>
        new Vue({
            el: "#box",
            data: {
                arr: [
                    { id: 1, name: "电视", price: 1990.00000 },
                    { id: 2, name: "电脑", price: 1880.00000 },
                    { id: 3, name: "电扇", price: 1770.00000 },
                ],
                arr2: [],
                quanxuan: false,
            },
            methods: {
                // 添加到购物车
                add(id) {
                    var a = this.arr.find(item => item.id == id)
                    var b = this.arr2.find(item => item.id == id)
                    if (b) {
                        b.num++
                    } else {
                        this.arr2.push({
                            id: a.id,
                            name: a.name,
                            price: a.price,
                            num: 1,
                            is: false,
                        })
                    }
                },
                // 数量加
                jia(id) {
                    var a = this.arr2.find(item => item.id == id)
                    a.num++
                },
                // 数量减
                jian(id) {
                    var a = this.arr2.find(item => item.id == id)
                    if (a.num > 1) {
                        a.num--
                    } else {
                        alert("商品数量不能低于1")
                    }
                },
                // 删除
                del(index) {
                    this.arr2.splice(index, 1)
                },
                // 全选
                xuan() {
                    this.arr2.forEach(item => {
                        item.is = this.quanxuan;
                    });
                },
                // 切换状态
                inps(id) {
                    var a = this.arr2.find(item => item.id == id)
                    a.is = !a.is
                },
                // 删除选中
                deltrue() {
                    for (let i = this.arr2.length - 1; i >= 0; i--) {
                        if (this.arr2[i].is) {
                            this.arr2.splice(i, 1);
                        }
                    }
                    this.quanxuan = false
                },
            },
            computed: {
                // 计算选中
                jisuan() {
                    var b = 0
                    this.arr2.forEach(item => {
                        if (item.is) {
                            b += item.num * item.price
                        }
                    })
                    return b
                }
            },
            filters: {
                n(num) {
                    return num + ".00"
                }
            }
        })
    </script>
</body>

</html>